(function($){
$(window).load(function(){
//判断用户是否登录，如果登录成功，则获取cookie中的用户名并显示到登录和注册的地方；
	if($.cookie('isLogin')=='true'){
	//	alert($.cookie('username'));
		$('.header-user').html('<a href="javascript:;" class="login">欢迎您，15201061391</a><a href="javascript:;" class="exit">[退出]</a><span class="line">|</span><a href="javascript:;" class="register">我的订单</a><span class="line">|</span><a href="javascript:;" class="register">购物车</a><span class="line">|</span><a href="javascript:;" class="register">我的收藏</a><span class="line">|</span><span class="ico-phone"></span>400-080-1888<span class="line">|</span><a href="javascript:;" class="da">下载达令<div class="download"><span class="arrow-7-5"></span><span class="pic-code"></span></div></a>')
	//点击退出按钮，清空cookie中的登录判断isLogin
		$('.exit').click(function(){
			$.cookie('isLogin',null,{path:'/'});
			window.location.href='index.html';
		})
	}
//判断cookie中购物车的数量，并同步到页面
$('.banner-cart-count').text($.cookie('count'));
//下载达令展示和隐藏图标
	$('.da').hover(function(){
		$('.download').css('display','block');
	},function(){
		$('.download').css('display','none');
	})
//导航栏全部分类移入移出事件
	$('.category .dd').css('display','block');
	$('.category').hover(function(){
		$('.icon-down').css('display','inline-block');
		$('.icon-up').css('display','none');
	},function(){
		$('.icon-down').css('display','none');
		$('.icon-up').css('display','inline-block');
	})
//后添加上的小米搜索框;
	var
		oSeachContent = $('.s-keyword'),
		oSearchList   = $('#search-list'),
		iSearchIndex  = -1,
		sOriHtml 	  = oSearchList.html();
	oSeachContent.focus(function () {
		oSearchList.css('display', 'block');
	}).blur(function () {
		oSearchList.css('display', 'none');
	}).keyup(function (ev) {
		var
			ev = ev || window.event,
			aLi = $('#search-list li');
		if(ev.keyCode === 38 || ev.keyCode === 40) {
			if(ev.keyCode === 38 && iSearchIndex > 0) {
				iSearchIndex--;
			} else if(ev.keyCode === 40 && iSearchIndex < aLi.length - 1) {
				iSearchIndex++;
			}
			oSeachContent.val(aLi.removeClass('active').eq(iSearchIndex).addClass('active').data('key'));
		}
	}).bind('input propertychange', function () {
		var sSearchCon = $(this).val();
		if(sSearchCon) {
			$.ajax({
				url: 'http://search.mi.com/search/expand',
				data: {keyword: sSearchCon},
				jsonp: 'jsonpcallback',
				dataType: 'jsonp',
				success: function (data) {
					var sHtml = '';
					data.forEach(function (v) {
						sHtml += '<li data-key="' + v.Key + '"><a href="javascript:;"><span>约有' + v.Rst + '件</span>' + v.Key + '</a></li>';
					});
					if(sHtml) {
						oSearchList.css('display', 'block').html(sHtml);
					} else {
						oSearchList.css('display', 'none');
					}
					iSearchIndex = -1;
				}
			});
		} else {
			oSearchList.html(sOriHtml);
			iSearchIndex = -1;
		}
	});
//banner轮播图
	var timer;
	var indexs = 0;
	//按钮列表移入效果
	$('.btn-pic').on('mouseover','li',
	function(){
		var inds = $(this).index();
					
		if(inds === indexs){
			return;
		}
		indexs = inds;
		move();
	    });
	//封装定时器
		times();
		function times(){
		timer = setInterval(function(){
		indexs ++;
					
		move();
		    },3000);
		}
	//封装函数move()
	function move(){
		if(indexs < 0){
			indexs = $('.btn-pic li').length - 1;
		}
		if(indexs >= $('.btn-pic li').length){
			indexs = 0;
		}
		$('.bw-pic li').stop(true).animate({
			opacity : 0,
			},500).eq(indexs).stop().animate({
				opacity : 1,
		},500);
		$('.btn-pic li').removeClass('active')
			.eq(indexs).addClass('active');
	}
//今日上新  购物车	
	$('.mtp2-dl').mouseover(function(){
		var inds = $(this);
		inds.find('.mtp2-dl-shop').css('display' , 'block');
	});
	$('.mtp2-dl').mouseout(function(){
		$(this).find('.mtp2-dl-shop').css('display' , 'none');
	});
//大家都说好 下拉菜单
	$('.mop-list').on('click','li',
	function(){
		var inds = $(this).index();
					
		if(inds === indexs){
			return;
		}
		indexs = inds;
					
		if(indexs < 0){
			indexs = $('.mop-list li').length - 1;
		}
		if(indexs >= $('.mop-list li').length){
			indexs = 0;
		}
		$('.main-our-pic div').removeClass('mop-div')
		.eq(indexs).addClass('mop-div');
		$('.mop-list li').removeClass('mop-list-li')
		.eq(indexs).addClass('mop-list-li');
	});
	//买了又买 下拉菜单
	
	$('.main-shops-left').on('mouseover','dl',
	function(){
		var indss = $(this).index();
//			console.log('--------------indss---------' + indss);
//			console.log('--------------this---------' + this);
		$('.main-shops-left dl').removeClass('msl-dl-over').eq(indss).addClass('msl-dl-over');
	});
	
	$('.main-shops-middle').on('mouseover','dl',
	function(){
		var indss = $(this).index();
//			console.log('--------------indss---------' + indss);
//			console.log('--------------this---------' + this);
		$('.main-shops-middle dl').removeClass('msl-dl-middle').eq(indss).addClass('msl-dl-middle');
	});
	
	$('.main-shops-right').on('mouseover','dl',
	function(){
		var indss = $(this).index();
//			console.log('--------------indss---------' + indss);
//			console.log('--------------this---------' + this);
		$('.main-shops-right dl').removeClass('msl-dl-right').eq(indss).addClass('msl-dl-right');
	});
	

	//闪购  查看更多  
	$('.mgp-left-hidd .mgp-left-more').click(function(){
		$('.mgp-left-d5').css('display' , 'block');
		$('.mgp-left-less').css('display','block');
	});
	$('.mgp-left-less').click(function(){
		$('.mgp-left-d5').css('display' , 'none');
		$('.mgp-left-less').css('display','none');
	});
	

	/*
	    3C家电  -------子页  列表  title
	 * */
	
	$('.lmtu-a2').click(
		function(){
	
		$('.lmtu-a2-style').css('display' , 'block');
	    }
	    
	);
	

	/*
	    3C家电  -------子页  列表   2  
	 * */
	
	$('.lmdu-lid-span2').click(
		function(){
	    $('.list-main-dian-ul').css({overflow : 'visible',height:'578px'});
		$('.lmdu-li2').css('overflow' , 'visible');
	    }
	    
	);
	$('.text-mul').click(
		function(){
		$('.list-main-dian-ul').css({overflow : 'visible',height:'206px'});		   
		$('.lmdu-li3').css('overflow' , 'visible').find('.lmdu-li-dd4').css('display','block');
		$('.lmdu-li-dd3').css('display','none');		    
	});	
//闪购右侧图片  轮播图   
	$.fn.scrollPlug = function(o){
    o = $.extend({  
        speed:      parseInt($(this).attr('speed')) || 30, // 滚动速度  
        step:       parseInt($(this).attr('step')) || 1, // 滚动步长  
        direction:  $(this).attr('direction') || 'up', // 滚动方向  
        pause:      parseInt($(this).attr('pause')) || 1000, // 停顿时长
		heightVle:  '',
		widthVle:   ''
    }, o || {});
	var dIndex = jQuery.inArray(o.direction, ['right', 'down']); 
	if (dIndex > -1) {  
        o.direction = ['left', 'up'][dIndex];  
        o.step = -o.step;  
    };
	var mid; 
	var ki          = 0;
    var div         = $('.mgp-right-list');
    var divWidth    = div.innerWidth();
    var divHeight   = div.innerHeight();  
    var ul          = $(".mrl-ul-ul", div);  
    var li          = $(".muu-li", ul);  
    var liSize      = li.size();  
    var liWidth     = o.widthVle  = li.width();
    var liHeight    = o.heightVle = li.height();
    var width       = liWidth * liSize;  
    var height      = liHeight * liSize;
	clearInterval(mid);	
	var scrollHandle = function() {// 滚动   
        if(o.direction == 'left'){  
            var l = div.scrollLeft(); 
			div.scrollLeft(l + o.step);
			ki += Math.abs(o.step);
            if(ki >= liWidth) _stop();  
        }else{
			var t = div.scrollTop();
			ki += Math.abs(o.step);
			div.scrollTop(t + o.step);
            if(ki >= liHeight) _stop();
        }; 
    };
	var _stop = function(){
		ki = 0;
		clearInterval(mid);	
	    };
	    mid = setInterval(scrollHandle, o.speed); 		
    };
	    $(function(){
		$('.Atag').click(function(){
			$("div[class]", $(this).parent()).scrollPlug({direction:this.rel,step:10});
			return false;
		});		   
	});
//闪购倒计时
function f1(){
		var mldtime = document.getElementById("mld-time-txt");
		var mldtime2 = document.getElementById("mld-time-txt2");
		var mldtime3 = document.getElementById("mld-time-txt3");
		var mldtime4 = document.getElementById("mld-time-txt4");
		var mldtime5 = document.getElementById("mld-time-txt5");
		var mldtime6 = document.getElementById("mld-time-txt6");
		var mldtime7 = document.getElementById("mld-time-txt7");
		var mldtime8 = document.getElementById("mld-time-txt8");
		var mldtime9 = document.getElementById("mld-time-txt9");
		var mldtime10 = document.getElementById("mld-time-txt10");
		var oDate = new Date();
	    var oYear = oDate.getFullYear();
	    var oNewDate = new Date(oYear, 11, 31, 23, 59, 59);   
	    var iTime = oNewDate.getTime()-oDate.getTime();
	    
	    var iS = iTime/1000;
		var iM = iS/60;
		var iH = iM/60;
		var iDate = parseInt(iH/24);
		var dd = iTime-iDate*24*60*60*1000;
		
		var s = parseInt(dd/1000);
		var m = parseInt(s/60);
		var h = parseInt(m/24);
		var s1 = s%60;
		var m1 = m%60;
		var h1 = h%24;
		if(h1>0 && h1<10){
			h1='0'+h1;
		}
		if(m1>0 && m1<10){
			m1='0'+m1;
		}
		if(s1>0 && s1<10){
			s1='0'+s1;
		}
		mldtime.value = h1+" : "+m1+" : "+s1;
	    mldtime2.value = h1+" : "+m1+" : "+s1;
	    mldtime3.value = h1+" : "+m1+" : "+s1;
	    mldtime4.value = h1+" : "+m1+" : "+s1;
	    mldtime5.value = h1+" : "+m1+" : "+s1;
	    mldtime6.value = h1+" : "+m1+" : "+s1;
	    mldtime7.value = h1+" : "+m1+" : "+s1;
	    mldtime8.value = h1+" : "+m1+" : "+s1;
		mldtime9.value = h1+" : "+m1+" : "+s1;
	    mldtime10.value = h1+" : "+m1+" : "+s1;    
}
		setInterval(f1,100);	
//右侧购物车侧边栏 
	

	//移到元素上背景色发生变化
	$('.sidebar-btn').hover(function(){
		$(this).css('background','#e34755')
	},function(){
		$(this).css('background','')
	})
	//点击购物车
	$('.sb-cart').toggle(function(){
		$('.sidebar').animate({'width':320},500,function(){
			$('.sb-cart').css('background','#e34755')
			$('.close').css('display','block')
		})
		
	},function(){
		$('.sidebar').animate({'width':40},500,function(){
			$('.sb-cart').css('background','')
		})
		$('.close').css('display','none')
	})
	//点击关闭按钮
	$('.close').click(function(){
		$('.sidebar').animate({'width':40},500,function(){
			$('.sb-cart').css('background','')
		})
		$('.close').css('display','none')
	})
	$('.sidebar-posi').hover(function(){
		$(this).find('strong').css('display','block');
	},function(){
		$(this).find('strong').css('display','none');
	})
	//回到顶部
	$('.goTop').click(function(){
		$('body').animate({'scrollTop':0},500)
	})
	
})
	
})(jQuery)